import * as ExtractTextPlugin from "extract-text-webpack-plugin";
import * as HtmlWebpackPlugin from "html-webpack-plugin";
import * as path from "path";
import * as webpack from "webpack";

export const outputDir = path.resolve(process.cwd(), "dist");
export const resolve = require.resolve;

export const babelPlugin = (name: string) => resolve(name, { paths: module.paths });

export const babelPreset = (name: string) => resolve(name, { paths: module.paths });

const babelPlugins = [
  [babelPlugin("babel-plugin-import"), { libraryName: "antd", style: "css" }],
  babelPlugin("babel-plugin-dynamic-import-webpack")
];

const babelPresets = [
  babelPreset("@babel/preset-typescript"),
  babelPreset("@babel/preset-react"),
  babelPreset("@babel/preset-env")
];

export const babelOptions = {
  babelrc: false,
  plugins: babelPlugins,
  presets: babelPresets
};

export const plugins = {
  extractText: new ExtractTextPlugin("[name].css") as any,
  htmlWebpack: new HtmlWebpackPlugin({
    filename: "index.html",
    template: "index.html",
    inject: true
  }) as any,
  envPlugin: new webpack.EnvironmentPlugin({
    NODE_ENV: "development",
    DEBUG: true
  }),
  commonsChunk: new webpack.optimize.CommonsChunkPlugin({
    name: "common"
  })
};

export const autoprefixer = require("autoprefixer")({ browsers: "last 2 versions, ie >= 9" });

export const rules = {
  css: {
    test: /\.css$/,
    use: plugins.extractText.extract({
      fallback: "style-loader",
      use: ["css-loader"]
    })
  },
  less: {
    test: /\.less$/,
    use: plugins.extractText.extract({
      fallback: "style-loader",
      use: [
        { loader: "css-loader", options: { minimize: true } },
        {
          loader: "postcss-loader",
          options: {
            ident: "postcss",
            plugins: () => [autoprefixer]
          }
        },
        {
          loader: "less-loader",
          options: {
            javascriptEnabled: true
          }
        }
      ]
    })
  },
  scss: {
    test: /\.scss$/,
    use: plugins.extractText.extract({
      fallback: "style-loader",
      use: [
        "css-loader",
        {
          loader: "postcss-loader",
          options: {
            ident: "postcss",
            plugins: () => [autoprefixer]
          }
        },
        "sass-loader"
      ]
    })
  },
  tsx: {
    test: /\.tsx?$/,
    exclude: (p: string) => {
      return /node_modules/.test(p) && !/xsite-editor|plume2/.test(p);
    },
    use: [
      {
        loader: "awesome-typescript-loader",
        options: {
          silent: true,
          useBabel: true,
          babelOptions,
          babelCore: resolve("@babel/core")
        }
      }
    ]
  },
  js: {
    test: /\.jsx?$/,
    loader: "babel-loader",
    exclude: (p: string) => {
      return /node_modules/.test(p) && !/x-site-ui|plume2/.test(p);
    },
    options: babelOptions
  },
  file: { test: /\.(jpe?g|gif|png|svg|woff|woff2|eot|ttf|otf|wav|mp3)$/, loader: "file-loader" }
};
